package externalflame.systemdesign.secondkill.dao;

import externalflame.systemdesign.secondkill.entity.SecKill;
import externalflame.systemdesign.secondkill.entity.StockInfo;
import org.apache.ibatis.annotations.*;


@Mapper
public interface SeckillDao {
    @Select("select id, product_id, stock, num_limit, start_time, end_time from seckill where id = #{secKillId}")
    SecKill get(@Param("secKillId") Long secKillId);

    @Insert("insert into seckill (id, product_id, stock, num_limit, start_time, end_time) values (#{secKill.id}, #{secKill.productId}, #{secKill.stock}, #{secKill.numLimit}, #{secKill.startTime}, #{secKill.endTime})")
    void insert(@Param("secKill") SecKill secKill);

    @Insert("insert into stock_info (product_id, stock, seckill_id) values (#{productId}, #{stock}, #{secKillId})")
    void insertStockInfo(@Param("id") Long id, @Param("productId") Long productId, @Param("stock") Long stock, @Param("secKillId") Long secKillId);

    @Update("<script>update seckill set <if test=\"secKill.productId != null and secKill.productId != ''\">product_id = #{secKill.productId},</if><if test=\"secKill.stock != null and secKill.stock != ''\">stock = #{secKill.stock},</if><if test=\"secKill.numLimit != null and secKill.numLimit != ''\">num_limit = #{secKill.numLimit},</if><if test=\"secKill.startTime != null and secKill.startTime != ''\">start_time = #{secKill.startTime},</if><if test=\"secKill.endTime != null and secKill.endTime != ''\">end_time = #{secKill.endTime},</if> id = #{secKill.id} where id = #{secKill.id}</script>")
    void update(@Param("secKill") SecKill secKill);

    @Update("<script>update stock_info set <if test=\"stockInfo.productId != null and stockInfo.productId != ''\">product_id = #{stockInfo.productId},</if><if test=\"stockInfo.stock != null and stockInfo.stock != ''\">stock = #{stockInfo.stock},</if>seckill_id = #{stockInfo.secKillId} where seckill_id = #{stockInfo.secKillId}</script>")
    void update(@Param("StockInfo") StockInfo stockInfo);
}
